package com.biz.primus.ms.oms.service.collection;

import com.biz.primus.common.utils.CopyUtils;
import com.biz.primus.model.oms.vo.*;
import com.biz.primus.model.oms.vo.returns.OmsReturnItemVo;
import com.biz.primus.model.oms.vo.returns.RefundRequestVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.oms.dao.po.OmsConsignee;
import com.biz.primus.ms.oms.dao.po.OmsOrder;
import com.biz.primus.ms.oms.dao.po.OrderPaymentInfo;
import com.biz.primus.ms.oms.dao.po.returns.OmsRefund;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturn;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturnItem;
import com.biz.primus.ms.oms.trans.ConsigneeConverter;
import com.biz.primus.ms.oms.trans.OrderConverter;
import com.biz.primus.ms.oms.trans.PaymentConverter;
import com.biz.primus.ms.oms.trans.ReturnConverter;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanweijin
 * @date 2017/11/3
 */
@Component
public class OrderDataConverter extends AbstractBaseService{


	@Autowired
	private ConsigneeConverter consigneeConverter;

	@Autowired
	private PaymentConverter paymentConverter;

	@Autowired
	private ReturnConverter returnConverter;

	@Autowired
	private OrderConverter orderConverter;

	public OmsOrder orderVo2Po(OmsOrderVo vo){
		return orderConverter.vo2po(vo);
	}

	public OmsOrderVo orderPo2Vo(OmsOrder order){
		return orderConverter.po2vo(order);
	}

	public OmsOrderRespVo  orderPo2RespVo(OmsOrder omsOrder){
		return orderConverter.po2RespVo(omsOrder);
	}

	public List<OrderPaymentInfo> paymentVo2Po(List<OmsPaymentVo> payments, Long orderId){
		return paymentConverter.vo2po(payments,orderId);
	}


	public OmsConsignee consigneeVo2Po(OmsConsigneeVo vo, Long orderId) {
		return consigneeConverter.vo2po(vo,orderId);
	}


	public OmsReturn returnVo2Po(OmsOrderReturnVo vo, OmsOrder order){
		return returnConverter.vo2po(vo, order);
	}


	public OmsRefund refundVo2Po(RefundRequestVo vo){
		OmsRefund omsRefund =new OmsRefund();
		omsRefund.setId(idService.getNextId());
		omsRefund.setRefundOperator(vo.getOperator());
		omsRefund.setReturnId(vo.getReturnId());
		omsRefund.setRefundAmount(vo.getRefundAmount());
		omsRefund.setOrderId(vo.getOrderId());
		omsRefund.setRefundBank(vo.getRefundBank());
		omsRefund.setRefundBankAccount(vo.getRefundBankAccount());
		omsRefund.setRefundReceiver(vo.getRefundReceiver());
		omsRefund.setRefundWays(vo.getRefundWays());
		omsRefund.setRemark(vo.getRemark());
		return omsRefund;
	}



	private OmsConsigneeVo consigneePo2Vo(OmsConsignee po) {
		return consigneeConverter.po2vo(po);
	}


	public OmsOrderRespVo omsrefund2RespVo(OmsRefund omsRefund,OmsOrderRespVo omsOrderRespVo){
		omsOrderRespVo.setRefundCode(omsRefund.getRefundCode());
		omsOrderRespVo.setRefundAmount(omsRefund.getRefundAmount());
		omsOrderRespVo.setRefundWays(omsRefund.getRefundWays());
		omsOrderRespVo.setRefundState(omsRefund.getRefundState().toString());
		omsOrderRespVo.setRefundCreateTime(omsRefund.getCreateTimestamp().toLocalDateTime());
		return omsOrderRespVo;
	}

	public OmsOrderRespVo omsreturn2RespVo(OmsReturn omsReturn,OmsOrderRespVo omsOrderRespVo){
		if(omsReturn!=null) {
			List<OmsReturnItem> omsReturnItems = omsReturn.getItems();
			List<OmsReturnItemVo> omsReturnItemVos = Lists.newArrayList();
			omsReturnItems.stream().map(req -> {
				OmsReturnItemVo omsReturnItemVo = new OmsReturnItemVo();
				CopyUtils.copyProperties(req, omsReturnItemVo);
				omsReturnItemVo.setReturnCode(req.getReturns().getReturnCode());
				omsReturnItemVo.setReturnState(req.getReturns().getReturnState());
				omsReturnItemVo.setCreateTime(req.getCreateTimestamp());
				return Lists.newArrayList(omsReturnItemVo);
			}).collect(Collectors.toList()).forEach(omsReturnItemVos::addAll);
			omsOrderRespVo.setOmsReturnItemVos(omsReturnItemVos);
		}
			return omsOrderRespVo;
	}

}
